<?php
/**
 * 用户抓取淘宝资讯
 * 
 * @version $Id$
 */
include_once('./simple_html_dom.php');
header("Content-Type:text/html;charset=utf-8");
if (function_exists('date_default_timezone_set')) 
	date_default_timezone_set('Asia/ShangHai');
	

///
$imageBase = dirname(dirname(__FILE__)).'/__wordpress/wp-content/uploads';
$imagePrefix = 'http://www.e35go.com/__wordpress/wp-content/uploads/';
$dbConf = array('localhost', 'linsir123', 'Lin369963$', 'linsir123');


///
$o = new tListSpider();
$o->run($imageBase, $imagePrefix, $dbConf);


/**
 * 列表
 */
class tListSpider
{
	/**
	 * @var 域名前缀
	 */
	public $domain = "http://fushi.taobao.com";
	
	/**
	 * @var 已抓取过的链接
	 */
	public $history;
	
	/**
	 * @var 分类Id
	 */
	public $categoryId;
	
	/**
	 * @var 分类Id
	 */
	public $cId;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$classes = array(
			array('潮流导购', 1189),
			array('搭配建议', 1634),
			array('靓包', 1200),
			array('美鞋', 1199)
		);
		$this->cId = intval($_GET['c']);
		if ( ! array_key_exists($this->cId, $classes)) 
			$this->cId = 0;
			
		///
		$this->categoryId = $classes[$this->cId][1];
		$this->history = $this->_getHistory();
	}
	
	/**
	 * 开始
	 */
	public function run($imageBase, $imagePrefix, $dbConf)
	{
		session_start();
		$key = "c_{$this->categoryId}";
		$data = $_SESSION[$key];
		if (empty($data)) {
			$list = $this->_getList();
			$data = $_SESSION[$key] = array(
				'index'=>0,
				'list'=>$list
			);
		}
		
		///
		echo "<pre>";
		print_r($data);
		echo "</pre>";
		
		///
		if ($data['index'] <= count($data['list'])-1) {
			$tmp = $data['list'][$data['index']];
			$url = $this->domain.str_replace('1.php', 'all.php#p1', $tmp);
			$data['index']++;
			$_SESSION[$key] = $data;
			
			///
			echo $url."<br /><br />";
			if ( ! in_array($tmp, $this->history)) {
				$o = new tArticleSpider($imageBase, $imagePrefix, $dbConf);
				$o->writeToWp($url);
				
				array_push($this->history, $tmp);
				$this->_setHistory();
			} else 
				echo '已抓取过';
				
			///
			$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
			echo '<html><head><meta http-equiv="refresh" content="2;URL=', $url, '" /></head></html>';
		} else 
			echo '抓取完毕';
	}
	
	/**
	 * 取得列表
	 */
	public function _getList()
	{
		$url = "http://fushi.taobao.com/list/{$this->categoryId}/1.php?tpl=waterfall-connector";
		$content = file_get_contents($url);
		$content = iconv("GBK", "UTF-8", $content);
		$strLen = strlen($content);
		$content = substr($content, 5, $strLen-7);
		$data = json_decode($content, true);
		
		///
		//print_r($data);
		//exit();
		
		///
		$list = array();
		if (is_array($data['data'])) {
			foreach ($data['data'] as $v) 
				$list[] = $v['publishUrl'];
		}
		
		return $list;
	}
	
	/**
	 * 取得历史
	 */
	public function _getHistory()
	{
		$file = dirname(__FILE__).'/__history.php';
		if ( ! file_exists($file)) 
			return array();
		else 
			return require($file);
	}
	
	/**
	 * 保存历史
	 */
	public function _setHistory()
	{
		$file = dirname(__FILE__).'/__history.php';
		$content = "<?php \r\n return ".var_export($this->history, true).";\r\n";
		file_put_contents($file, $content);
	}
}




/**
 * 文章
 */
class tArticleSpider
{
	/**
	 * @var 当前页面地址
	 */
	public $url;
	
	/**
	 * @var “文章”DOM对象
	 */
	public $oArticle;
	
	/**
	 * @var 数据库配置
	 */
	public $dbConf;
	
	/**
	 * @var 图片存放相对目录
	 */
	public $imageFolder;
	
	/**
	 * @var 图片文件名
	 */
	public $imageName;
	
	/**
	 * @var 图片存放根地址
	 */
	public $imageBase;
	
	/**
	 * @var 图片WEB地址前缀
	 */
	public $imagePrefix;
	
	/**
	 * @var 图片抓取状态
	 */
	public $imageFlag = false;
	
	/**
	 * 构造函数
	 */
	public function __construct($imageBase, $imagePrefix, $dbConf)
	{
		$year = date('Y');
		$month = date('m');
		$day = date('d');
		$d = DIRECTORY_SEPARATOR;
		$this->imagePrefix = $imagePrefix;
		$this->imageFolder = "{$year}/{$month}/{$day}/";
		$this->imageBase = $imageBase.$d.$year.$d.$month.$d.$day.$d;
		$this->dbConf = $dbConf;
		$this->_dirMk($this->imageBase);
	}
	
	/**
	 * 初始化“文章”DOM对象
	 */
	public function _initOArticle($url)
	{
		$this->url = $url;
		$this->oArticle = file_get_html($url);
		if (empty($this->oArticle)) 
			throw new Exception("抓取有误", 1000);
	}
	
	/**
	 * 文章标题
	 */
	public function _getATitle()
	{
		$s = $this->oArticle->find('div.info-big-title h1', 0);
		return $this->_iconv($s->innertext);
	}
	
	/**
	 * 文章摘要
	 */
	public function _getASummary()
	{
		$s = $this->oArticle->find('div.info-abstract', 0);
		return $this->_iconv($s->innertext);
	}
	
	/**
	 * 文章主体
	 */
	public function _getABody()
	{
		$s = $this->oArticle->find('div#art-content', 0);
		foreach($s->find('table') as $e)
			$e->outertext = '';
		
		foreach($s->find('div.ke_items') as $e)
			$e->outertext = '';
			
		foreach($s->find('div[style]') as $e)
			$e->outertext = '';
			
		$i = 0;
		foreach($s->find('img') as $e) {
			if (++$i > 1) 
				$e->outertext = '';
			else {
				$this->_fetchImg($e->src);
				$src = $this->imagePrefix.$this->imageFolder.$this->imageName;
				$e->src = $src;
			}
		}
			
		$s = $this->_iconv($s->innertext);
		$s = preg_replace('/<p style=\"text-align:\s?(left|right|center);\">(&nbsp;)+<\/p>/i', '', $s);
		$s = preg_replace('/<\w+>\s*(&nbsp;)+\s*<\/\w+>/', '', $s);
		return $s;
	}
	
	/**
	 * 插入内容
	 */
	public function writeToWp($url)
	{
		try {
			$this->_initOArticle($url);
		} catch (Exception $e) {
			return ;
		}
		
		///
		$title = $this->_getATitle();
		$summary = $this->_getASummary();
		$body = $this->_getABody();
		$content = "<div class='summary'>{$summary}</div><div class='body'>{$body}</div>";
		///
		
		$time = date('Y-m-d H:i:s', time()-24*3600);
		$data = array(
			'post_author' => 1,
			'post_date' => $time,
			'post_date_gmt' => $time,
			'post_content' => $content,
			'post_title' => $title,
			'post_status' => 'publish',
			'comment_status' => 'open',
			'post_name' => $title,
			'post_modified' => $time,
			'post_modified_gmt' => $time,
			'guid' => '',
			'post_type' => 'post',
		);
		$postId = $this->_dbInsert('wp_posts', $data);
		
		///
		if ($this->imageFlag) {
			$data['post_content'] = '';
			$data['post_status'] = 'inherit';
			$data['post_type'] = 'attachment';
			$data['post_mime_type'] = 'image/jpeg';
			$data['post_title'] = $this->imageName;
			$data['post_name'] = $this->imageName;
			$data['post_parent'] = $postId;
			$postId = $this->_dbInsert('wp_posts', $data);
			
			$data = array(
				'post_id' => $postId,
				'meta_value' => $this->imageFolder.$this->imageName,
				'meta_key' => '_wp_attached_file',
			);
			$this->_dbInsert('wp_postmeta', $data);
		}
	}
	
	/**
	 * 数据库查询
	 */
	public function _dbInsert($table, $data)
	{
		static $cc = null;
		if ( ! isset($cc)) {
			$cc = mysql_connect($this->dbConf[0], $this->dbConf[1], $this->dbConf[2]);
			mysql_select_db($this->dbConf[3]);
			mysql_query("SET NAMES utf8");
		}
		
		$fields = array_keys($data);
		$fields = $this->_dbQuote($fields);
		$data = $this->_dbSqlEscape($data);
		$format = "INSERT INTO %s (%s) VALUES (%s)";
		$sql = sprintf($format, $table, implode(',', $fields), implode(',', $data));
		print_r($sql);
		echo "<br /><br />";
		
		///
		mysql_query($sql);
		return mysql_insert_id();
	}
	
	/**
	 * SQL引号
	 */
	public function _dbQuote($var)
	{
		$var = is_array($var) ?
			array_map(array($this, '_dbQuote'), $var) :
			'`'.trim($var).'`';
		
		return $var;
	}
	
	/**
	 * 转义SQL变量
	 */
	public function _dbSqlEscape($var)
	{
		if (is_bool($var)) 
			$var = $var ? 1 : 0;
		elseif (is_null($var)) 
			$var = 'NULL';
		elseif (is_string($var))
			$var = "'".mysql_escape_string($var)."'";
		elseif (is_array($var))
			$var = array_map(array($this, '_dbSqlEscape'), $var);
			
		return $var;
	}
	
	/**
	 * 过滤目录
	 */
	public function _dirSafe($dir)
	{
		if (substr($dir, -1) == DIRECTORY_SEPARATOR) 
			$dir = substr($dir, 0, -1);
			
		return $dir;
	}
	
	/**
	 * 级联创建目录
	 */
	public function _dirMk($dir, $mode = 0777)
	{
		$dir = $this->_dirSafe($dir);
		if (file_exists($dir) && is_dir($dir)) 
			return true;
		
		///
		$this->_dirMk(dirname($dir), $mode);
		
		if (mkdir($dir)) 
			return chmod($dir, $mode);
		
		return false;
	}
	
	/**
	 * 抓取图片
	 */
	public function _fetchImg($url)
	{
		$t = parse_url($url);
		$host = $t['host'];
		$uri = $t['path'];
		$port = 80;
		
		///
		$crlf = "\r\n"; 
		$req = 'GET '. $uri .' HTTP/1.1' . $crlf; 
		$req .= 'Host: '. $host . $crlf; 
		$req .= 'User-Agent: Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)' . $crlf; 
		$req .= 'Accept: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf; 
		$req .= 'Accept-Language: zh-cn,zh;q=0.5' . $crlf; 
		$req .= 'Referer: http://fushi.taobao.com/' . $crlf; 
		$req .= 'Accept-Encoding: gzip, deflate' . $crlf; 
		$req .= 'Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7' . $crlf; 
		$req .= $crlf;
		
		if (($fp = @fsockopen($host, $port, $errno, $errstr)) == false) 
			return false; 
			
		stream_set_timeout($fp, 5, $timeout * 1000); 
		fputs($fp, $req); 
		while ($line = fgets($fp)) $ret .= $line; 
		fclose($fp); 
		
		if (!$res_hdr) 
			$ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); 
			
		///
		if ($ret) {
			$this->imageFlag = true;
			$this->imageName = md5($this->url).'.jpg';
			$fileName = $this->imageBase.$this->imageName;
			file_put_contents($fileName, $ret);
		}
	}
	
	/**
	 * 编码转换
	 */
	public function _iconv($text)
	{
		return iconv('GB2312', 'UTF-8', $text);
	}
}
